//
//  HomeTableViewCell.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/26.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class HomeTableViewCell: UITableViewCell {
    
    var avatarView:UIImageView!//头像
    var userName:UILabel!//用户名
    var rankLabel:UILabel!//排名
    var informationLabel:UILabel!//说明信息    阅读数和满意率
    var questionIcon:UIImageView!//问 小图标
    var questionLabel:UILabel!//
    var subjectLabel:UILabel!//主题标签
    var previewImage:UIImageView!//预览图片
    var rowHeight:CGFloat! = 0
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        avatarView = UIImageView()
        userName = UILabel()//用户名
        rankLabel = UILabel()
        informationLabel = UILabel()//说明信息  右上角  阅读数和满意率
        questionIcon = UIImageView()//问 小图标
        questionLabel = UILabel()//
        subjectLabel = UILabel()
        previewImage = UIImageView()
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .None
        self.backgroundColor = UIColor.whiteColor()
        self.addAvatarImageView()
        self.addUserName()
        self.createRankLabel()
        self.createQuestionIcon()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK:--UI设置
extension HomeTableViewCell{
    //添加头像
    func addAvatarImageView(){
        avatarView.frame = CGRect(x: 15*SCALEW, y: 15*SCALEH, width: 30*SCALEW, height: 30*SCALEW)
        self.addSubview(avatarView)
        
        //在头像上面添加一层中间有圆形透明的view  使头像变成圆形的
        let cornerRadiusView = UIImageView(frame: CGRectMake(-1, -1, 32*SCALEW, 32*SCALEW))
        cornerRadiusView.image = UIImage(named: "makeImagecornerRadiusW")
        avatarView.addSubview(cornerRadiusView)
    }
    
    //添加用户名
    func addUserName(){
        userName.frame = CGRect(x: 60*SCALEW,y: 14*SCALEH,width: 148*SCALEW,height: 14*SCALEH)
        userName.font = BaseFunc.getSystemFont(12)
        userName.textColor = UIColor.blackColor()
        userName.textAlignment = .Left
        self.addSubview(userName)
    }
    
    //添加排名
    func createRankLabel(){
        rankLabel.frame = CGRect(x: 60*SCALEW,y: 30*SCALEH,width: 148*SCALEW,height: 14*SCALEH)
        rankLabel.font = BaseFunc.getCustomFont(12)
        rankLabel.textColor = BaseFunc.createCustomColor(0, b: 65, g: 255, a: 1)
        rankLabel.textAlignment = .Left
        self.addSubview(rankLabel)
    }
    
    //question的小图标
    func createQuestionIcon(){
        questionIcon = UIImageView(frame: CGRect(x: 17*SCALEW, y: 55*SCALEH, width: 20*SCALEW, height: 20*SCALEW))
        questionIcon.image = UIImage(named: "question")
        self.addSubview(questionIcon)
    }
    
    //question
    func createQuestionLabel(text:String){
        questionLabel.font = UIFont(name: "Heiti SC", size: 15*SCALEW)
        questionLabel.textColor = UIColor.blackColor()
        questionLabel.numberOfLines = 0
        questionLabel.textAlignment = .Left
        questionLabel.text = text
        self.addSubview(questionLabel)
        
        let text = questionLabel.text
        let constraint = CGSize(width: 205*SCALEW, height: 10000)
        let size = text!.boundingRectWithSize(constraint, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes:NSDictionary(object:UIFont(name: "Heiti SC", size: 15*SCALEW)!, forKey: NSFontAttributeName as NSCopying) as? [String : AnyObject],context: nil)
        questionLabel.frame = CGRect(x: 46*SCALEW,y: 55*SCALEH,width: 209*SCALEW,height: size.height)
        
        rowHeight = 55*SCALEH+size.height+15*SCALEH//确定标签的y坐标
    }
    
    
    //添加专题标签 board  and  label
    func createSubjectIcon(subjectText:String){
        subjectLabel.font = UIFont(name: "Heiti SC", size: 12*SCALEW)
        subjectLabel.textColor = Theme.BlueColor
        subjectLabel.textAlignment = .Center
        subjectLabel.text = subjectText
        self.addSubview(subjectLabel)
        
        let text = subjectLabel.text
        let constraint = CGSize(width: 1000, height: 1000)
        let size = text!.boundingRectWithSize(constraint, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes:NSDictionary(object:UIFont(name: "Heiti SC", size: 12*SCALEW)!, forKey: NSFontAttributeName as NSCopying) as? [String : AnyObject],context: nil)
        subjectLabel.frame = CGRect(x: 17*SCALEW,y: rowHeight,width: size.width+12*SCALEW,height: 19*SCALEH)
        subjectLabel.layer.borderWidth = 1
        subjectLabel.layer.borderColor = Theme.BlueColor.CGColor
        subjectLabel.layer.cornerRadius = 4
        
        
        self.createInformationLabel(20*SCALEW+size.width+20*SCALEW)
        
    }
    
    
    //添加阅读率和付款满意率
    func createInformationLabel(x:CGFloat){
        informationLabel.frame = CGRect(x: x,y: rowHeight,width: 170*SCALEW,height: 19*SCALEH)
        informationLabel.font = BaseFunc.getCustomFont(11)
        informationLabel.textColor = Theme.GrayColor
        informationLabel.textAlignment = .Left
        self.addSubview(informationLabel)
        
        rowHeight = rowHeight + 40*SCALEH
        self.createCutLine()
        self.createPreviewImage()
    }
    
    //创建分割条
    func createCutLine(){
       let cutLine = UIView(frame: CGRect(x: 0,y: rowHeight-5,width: WidthAPP,height: 5))
        cutLine.backgroundColor = Theme.BGColor
        self.addSubview(cutLine)
    }
    
    
    //创建预览图片
    func createPreviewImage(){
        previewImage.frame = CGRect(x: 266*SCALEW, y: rowHeight/2-47*SCALEW, width: 94*SCALEW, height: 94*SCALEW)
        previewImage.contentMode = .ScaleAspectFit
        self.addSubview(previewImage)
    }
}











